﻿using HoverTree.HoverTreeFrame;
using HoverTree.Model;
using System.IO;
using System.Text;
using System.Web;

namespace HoverTree.BLL
{
    class HvtPictureGenerate
    {
        /// <summary>
        ///  模板内容
        /// </summary>
        string _hvtTemplateContent = "";

        /// <summary>
        /// 获取模板内容
        /// </summary>
        /// <returns></returns>
        string GetTemplate(string hvtTemplateFile)
        {
            if (hvtTemplateFile == string.Empty)
                hvtTemplateFile = "hoverTreeTemplate.htm";

            StreamReader m_sr = new StreamReader(Utils.GetMapPath("/hvttemplate/" + hvtTemplateFile), Encoding.UTF8);
            _hvtTemplateContent = m_sr.ReadToEnd();
            m_sr.Close();

            return _hvtTemplateContent;
        }

        string GetTemplate()
        { return GetTemplate(""); }

        /// <summary>
        /// 构造页面内容
        /// </summary>
        /// <param name="info">当前信息</param>
        /// <param name="lastInfo">上一信息</param>
        /// <param name="nextInfo">下一信息</param>
        /// <returns></returns>
        string MakePageContent(PictureInfo info, PictureInfo lastInfo, PictureInfo nextInfo)
        {
            ImageSetInfo m_isInfo = new ImageSetInfo();
            ImageSetSelect m_isSelect = new ImageSetSelect();
            m_isSelect.HvtID = info.HvtImageSetId;
            m_isInfo= new HvtImageSet().Get(m_isSelect);
            

            string m_pageContent = GetTemplate();
            m_pageContent = m_pageContent.Replace("[HvtTitle/]", HttpContext.Current.Server.HtmlEncode(info.HvtTitle));
            m_pageContent = m_pageContent.Replace("[HvtSrc/]", HttpContext.Current.Server.HtmlEncode(info.HvtSrc));
            if (nextInfo != null)
                m_pageContent = m_pageContent.Replace("[HvtNextURL/]", nextInfo.HvtURL);
            else
                m_pageContent = m_pageContent.Replace("[HvtNextURL/]","javascript:;");
            //图集标题
            m_pageContent = m_pageContent.Replace("[HvtImageSetTitle/]", HttpContext.Current.Server.HtmlEncode(m_isInfo.HvtTitle));
            m_pageContent = m_pageContent.Replace("[HvtLastLink/]", MakeLastLink(lastInfo));
            m_pageContent = m_pageContent.Replace("[HvtNextLink/]", MakeNextLink(nextInfo));
            m_pageContent = m_pageContent.Replace("[图集图片列表/]", "<a href=\"/hvtimg/hvtpiclist.aspx?isid="+info.HvtImageSetId.ToString()+"\">查看图集全部图片</a>");
            m_pageContent = m_pageContent.Replace("[跳转链接/]", MakeLink (info.HvtLink));

            return m_pageContent;
        }

        string MakeLink(string link)
        {
            if (link == string.Empty)
                return string.Empty;
            return "<a href=\"" + link + "\" target=\"_blank\">去看看</a>";
        }



        string MakeLastLink(PictureInfo info)
        {
            if (info == null)
                return "";
            else
                return "<a href = \"" + info.HvtURL + "\" class=\"hvtlast\" ><<</a>";
        }

        string MakeNextLink(PictureInfo info)
        {
            if (info == null)
                return "";
            else
                return "<a href = \"" + info.HvtURL + "\" class=\"hvtnext\" >>></a>";
        }

        /// <summary>
        /// 生成页面
        /// </summary>
        /// <param name="info">当前信息</param>
        /// <param name="lastInfo">上一信息</param>
        /// <param name="nextInfo">下一信息</param>
        /// <returns></returns>
        public string Generate(PictureInfo info, PictureInfo lastInfo, PictureInfo nextInfo)
        {
            if (info == null)
                return "未生成，图片不存在或者不公开显示";

            Utils.CreateFile(MakePageContent(info, lastInfo, nextInfo), Utils.GetMapPath(info.HvtURL), Encoding.UTF8);
            return "";
        }

        
    }
}